﻿using UnityEngine;

namespace IQIGame.Onigao.GameAOT
{
    public class HUDNumberEntry
    {
        public Transform m_tf;
        public HUDNumberEntry m_pNext;
        public int m_nType;
        public Vector3 m_vPos; // 对象的世界坐标
        public Vector2 m_vScreenPos; // 屏幕坐标
        public Vector2 m_ScreenDir; //屏幕移动方向
        public Vector2 m_vInitOffset; // 初始化位移
        public Vector2 m_vMove; // 位移
        public Vector2 m_vMoveDis;   //需要位移的距离
        public float m_fAniScale; // 动画缩放
        public float m_fScale;
        public float m_fAlpha; // 半透
        public BetterList<HUDVertex> m_aSprite = new BetterList<HUDVertex>(); // 对应的图片ID
        public int m_nWidth = 0; // 宽度
        public int m_nHeight = 0;
        public int m_nSpriteGap = 0;
        public float m_fStartTime = 0.0f;
        public bool m_bStop = false;

        public void reset()
        {
            m_tf = null;
            m_vPos = Vector3.zero;
            m_vScreenPos = Vector2.zero;
            m_ScreenDir = Vector2.one;
            m_vInitOffset = Vector2.zero;
            m_vMove = Vector2.zero;
            m_fScale = 1.0f;
            m_fAniScale = 1.0f;
            m_fAlpha = 1.0f;
            ReleaseVertex();
            m_nWidth = 0;
            m_nHeight = 0;
            m_nSpriteGap = 0;
            m_bStop = false;
        }

        public void ReleaseVertex()
        {
            for (int i = m_aSprite.size - 1; i >= 0; --i)
            {
                HUDVertex v = m_aSprite[i];
                if (v.hudMesh != null)
                    v.hudMesh.EraseHUDVertex(v);
                v.hudMesh = null;
                HUDVertex.ReleaseVertex(v);
                m_aSprite[i] = null;
            }

            m_aSprite.Clear();
        }

        // 功能：更新屏幕位置
        public bool UpdateScreenPos(HUDNumberSetting attrib)
        {
            // if (attrib.ScreenAlign)
            //     return false;

            // 暂时不需要实时跟随Transform
            // if (m_tf != null)
            // {
            //     Vector3 vPos = m_tf.position;
            //     Vector3 v = vPos - m_vPos;
            //     if (v.x * v.x + v.y * v.y + v.z * v.z > 0.00001f)
            //     {
            //         Camera caMain = HUDGlobal.GetHUDMainCamera();
            //         if (caMain != null)
            //         {
            //             m_vPos = vPos;
            //             m_vScreenPos = caMain.WorldToScreenPoint(vPos);
            //             Vector3 vCameraPos = caMain.transform.position;
            //             // CaleCameraScale(vCameraPos);
            //             return true;
            //         }
            //     }
            // }

            return false;
        }

        public void CaleCameraScale(Vector3 vCameraPos, float scaleRatio, (float, float, float, float) rGlobalData)
        {
            Vector3 vPos = m_vPos;
            float m_nearDistance = rGlobalData.Item1;//     HUDGlobal.CameraNearDist;
            float m_farDistance = rGlobalData.Item2;//      HUDGlobal.CameraFarDist;
            float m_minScale = rGlobalData.Item3;//         HUDGlobal.NumberScaleMin;
            float m_maxScale = rGlobalData.Item4;//         HUDGlobal.NumberScaleMax;
            float dis = Vector3.Distance(vPos, vCameraPos);
            float ratio = Mathf.Clamp01((dis - m_nearDistance) / (m_farDistance - m_nearDistance));
            float fScale = m_minScale * ratio + (1.0f - ratio) * m_maxScale;
            m_fScale = (1.0f / fScale) * scaleRatio;
        }

        public void PushSprite(float y, Sprite sp,Color color)
        {
            HUDVertex node = HUDVertex.QueryVertex();
            node.WorldPos = m_vPos;
            // node.ScreenPos = m_vScreenPos;
            node.Offset.Set(m_nWidth, y);
            node.NumSprite = sp;
            node.InitSprite(color);
            m_aSprite.Add(node);
            m_nWidth += node.width + m_nSpriteGap;
            if (m_nHeight < node.height)
                m_nHeight = node.height;
        }

        public void MakeLeft()
        {
            m_nWidth -= m_nSpriteGap;
            MoveAll(0.0f);
        }

        // 居中
        public void MakeCenter()
        {
            m_nWidth -= m_nSpriteGap;
            float fHalfW = m_nWidth * 0.5f;
            MoveAll(fHalfW);
        }

        // 右对齐
        public void MakeRight()
        {
            m_nWidth -= m_nSpriteGap;
            float fMoveX = m_nWidth;
            MoveAll(fMoveX);
        }

        void MoveAll(float fMoveX)
        {
            float fHalfH = m_nHeight * 0.5f;
            for (int i = 0, nSize = m_aSprite.size; i < nSize; ++i)
            {
                float fH = m_aSprite[i].height;
                m_aSprite[i].Offset.x -= fMoveX;
                m_aSprite[i].Offset.y -= fH * 0.5f - fHalfH;
            }
        }
    }
}